package example.cdx.com.textureviewdemo.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.TextureView;

import butterknife.BindView;
import butterknife.ButterKnife;
import example.cdx.com.textureviewdemo.R;

public class Test1Activity extends AppCompatActivity {

    private static final String TAG = Test1Activity.class.getSimpleName();

    @BindView(R.id.texture)
    TextureView texture;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test1);

        initParamsAndValus();

        initView();

        initData();
    }

    private void initParamsAndValus() {
        mContext = this;
    }

    private void initView() {
        ButterKnife.bind(this);
        texture.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            //surface 创建成功
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Log.e(TAG,"onSurfaceTextureAvailable");
                drawContent();
            }

            //surface 大小发生变化
            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                Log.e(TAG,"onSurfaceTextureSizeChanged");
            }

            //surface 被销毁
            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                Log.e(TAG,"onSurfaceTextureDestroyed");
                return false;
            }

            //surface 更新
            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
                Log.e(TAG,"onSurfaceTextureUpdated");
            }
        });
    }

    private void initData() {

    }

    private void drawContent() {
        //设置画笔
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        //锁定画布，并且绘制圆
        //这个逻辑和SurfaceView的逻辑相似，都是先锁定Canvas，然后最后提交
        Canvas canvas = texture.lockCanvas();
        canvas.drawColor(Color.WHITE);
        canvas.drawCircle(200,300,100,paint);
        texture.unlockCanvasAndPost(canvas);
    }
}
